package com.wxdc.nmgyt.nmgytwydc.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.wxdc.nmgyt.nmgytwydc.R;
import com.wxdc.nmgyt.nmgytwydc.activity.AuditBackDataActivity;
import com.wxdc.nmgyt.nmgytwydc.activity.LoginActivity;
import com.wxdc.nmgyt.nmgytwydc.activity.ToCommitDataActivity;
import com.wxdc.nmgyt.nmgytwydc.activity.WaitCommitActivity;
import com.wxdc.nmgyt.nmgytwydc.app.MyApp;
import com.wxdc.nmgyt.nmgytwydc.utils.FileUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/3/21.
 */

public class NavigationMeFragment extends Fragment implements BaseQuickAdapter.OnItemClickListener {
    Unbinder unbinder;

    @BindView(R.id.manager_save)
    TextView mManagerSave;
    @BindView(R.id.manager_save_content)
    ScrollView mManagerSaveContent;
    @BindView(R.id.manager_me)
    TextView mManagerMe;
    @BindView(R.id.manager_me_content)
    ScrollView mManagerMeContent;
    @BindView(R.id.manager_save_recycler)
    RecyclerView mManagerSaveRecycler;
    @BindView(R.id.toolbar_title)
    TextView mToolbarTitle;

    private List<String> mStringsList = new ArrayList<>();
    private RecyclerAdapter mRecycleraAdapter;
    private AppCompatActivity mActivity;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (AppCompatActivity) context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View view = inflater.inflate(R.layout.fragment_me, container, false);
        unbinder = ButterKnife.bind(this, view);
        String filePath = FileUtil.OpenfilePath;// Environment.getExternalStorageDirectory().getAbsolutePath() + "/jczApp/gpsDataFile/";
        mStringsList = getFiles(filePath);
        mManagerSaveRecycler.setLayoutManager(new LinearLayoutManager(mActivity));
        mManagerSaveRecycler.setAdapter(mRecycleraAdapter = new RecyclerAdapter(mStringsList));
        mRecycleraAdapter.setOnItemClickListener(this);
        mToolbarTitle.setText(getResources().getString(R.string.bottom_bar_title4));
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    /* 获取文件夹下所有文件*/
    public List<String> getFiles(String path) {
        mStringsList.clear();
        File file = new File(path);
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].getAbsolutePath().contains(".txt")) {
                mStringsList.add(files[i].getName());//.getAbsolutePath());
            }
        }
        return mStringsList;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        String filePath = FileUtil.OpenfilePath;
        String fileName = mStringsList.get(position);
        File f = new File(filePath + fileName);
        openFile(f);
    }

    @OnClick(R.id.manager_exit)
    public void onViewClicked() {
        startActivity(new Intent(getActivity(), LoginActivity.class));
    }

    @OnClick({R.id.manager_commit_data, R.id.manager_collection, R.id.manager_title_gps, R.id.manager_title_me})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.manager_commit_data:
                WaitCommitActivity.startActivity(mActivity);
                break;
            case R.id.manager_collection:
                //MyApp.getInstance().showToastLongMessage(getActivity(), "ssdddds");
                AuditBackDataActivity.startActivity(mActivity);
                break;
            case R.id.manager_title_gps:
                if (mManagerSaveContent.getVisibility() == View.VISIBLE) {
                    mManagerSave.setText("▶");
                    mManagerSave.setTextColor(this.getResources().getColor(R.color.grgray));
                    mManagerSaveContent.setVisibility(View.GONE);
                } else if (mManagerSaveContent.getVisibility() != View.VISIBLE) {
                    mManagerSave.setText("▼");
                    mManagerSave.setTextColor(this.getResources().getColor(R.color.drak_green));
                    mManagerSaveContent.setVisibility(View.VISIBLE);
                    mManagerMeContent.setVisibility(View.GONE);
                    mManagerMe.setText("▶");
                    mManagerMe.setTextColor(this.getResources().getColor(R.color.grgray));

                }
                break;
            case R.id.manager_title_me:
                if (mManagerMeContent.getVisibility() == View.VISIBLE) {
                    mManagerMe.setText("▶");
                    mManagerMe.setTextColor(this.getResources().getColor(R.color.grgray));
                    mManagerMeContent.setVisibility(View.GONE);
                } else if (mManagerMeContent.getVisibility() != View.VISIBLE) {
                    mManagerMe.setText("▼");
                    mManagerMe.setTextColor(this.getResources().getColor(R.color.drak_green));
                    mManagerMeContent.setVisibility(View.VISIBLE);
                    mManagerSaveContent.setVisibility(View.GONE);
                    mManagerSave.setText("▶");
                    mManagerSave.setTextColor(this.getResources().getColor(R.color.grgray));
                }
                break;
        }
    }

    public class RecyclerAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
        public RecyclerAdapter(@Nullable List<String> data) {
            super(R.layout.recycler_activity, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            helper.setText(R.id.recycler_01, item);

        }
    }


    /* 打开文件*/
    private void openFile(File file) {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //设置intent的Action属性
        intent.setAction(Intent.ACTION_VIEW);
        //获取文件file的MIME类型
        String type = getMIMEType(file);
        //设置intent的data和Type属性。
        intent.setDataAndType(/*uri*/Uri.fromFile(file), type);
        //跳转
        startActivity(intent);
    }


    /*根据文件后缀名获得对应的MIME类型。*/
    private String getMIMEType(File file) {

        String type = "*/*";
        String fName = file.getName();
        //获取后缀名前的分隔符"."在fName中的位置。
        int dotIndex = fName.lastIndexOf(".");
        if (dotIndex < 0) {
            return type;
        }
   /* 获取文件的后缀名*/
        String end = fName.substring(dotIndex, fName.length()).toLowerCase();
        if (end == "") return type;
        //在MIME和文件类型的匹配表中找到对应的MIME类型。
        for (int i = 0; i < MIME_MapTable.length; i++) {
            if (end.equals(MIME_MapTable[i][0]))
                type = MIME_MapTable[i][1];
        }
        return type;
    }

    private final String[][] MIME_MapTable = {
            //{后缀名，MIME类型}
            {".tgz", "application/x-compressed"},
            {".txt", "text/plain"},
            {"", "*/*"}
    };

    @Override
    public void onDetach() {
        super.onDetach();
        mActivity = null;
    }
}
